﻿using db_manage.dll.common;
using db_manage.dll.manage;
using db_manage.model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace db_manage.page
{
    public partial class form_db_info : Form
    {
        private Form form_db_info_filter { get => manage.from_get<form_db_info_filter>(); }

        private List<db_info> _db_info_list
        {
            get => manage.db_info_list_get();
        }

        private bool show_fail = false;//仅显示失败数据
        private bool if_stop = false;//是否暂停

        public form_db_info()
        {
            InitializeComponent();
            data_update();
            config_get();
            new Thread(new ThreadStart(() =>
             {
                 while (true)
                 {
                     Thread.Sleep(2000);
                     Action action = () =>
                     {
                         data_update();
                     };
                     if (this.IsDisposed)
                     {
                         break;
                     }
                     this?.Invoke(action);
                 }
             })).Start();
        }

        private void config_get()
        {
            if (manage.db_manage_config.task_num < 1)
            {
                manage.db_manage_config.task_num = 1;
            }
            tb_task_num.Text = manage.db_manage_config.task_num.ToString();
        }

        private void config_set()
        {
            manage.db_manage_config.task_num = Convert.ToInt32(tb_task_num.Text ?? "1");
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        public void data_update(Boolean isNew = false)
        {
            var now_list = _db_info_list;
            if (show_fail)
            {
                now_list = now_list.Where(x => x.updatestatus == 0).ToList() ?? new List<db_info>();
            }

            if (isNew ||
                this.dgv_db_info.DataSource == null ||
                now_list.Count() != ((List<db_info>)this.dgv_db_info.DataSource).Count())
            {
                this.dgv_db_info.DataSource = new List<db_info>(now_list);
            }
            else
            {
                this.dgv_db_info.Refresh();
            }

            var check_list = now_list.Where(x => x.is_check).ToList();
            var total = check_list.Count();
            var finish = check_list.Where(x => x.updatestatus != null).Count();
            l_total.Text = "数据库数量:" + now_list.Count();
            l_check.Text = "勾选数量:" + total;
            l_finish_value.Text = finish.ToString();//完成数量
            l_success_value.Text = check_list.Where(x => x.updatestatus == 1).Count().ToString();//成功数量
            l_fail_value.Text = check_list.Where(x => x.updatestatus == 0).Count().ToString();//失败数量
            pb_finish.Maximum = total;
            pb_finish.Value = finish;//进度
        }

        /// <summary>
        /// 获取所有数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_db_list_get_Click(object sender, EventArgs e)
        {
            manage.db_info_list_get(true);
            data_update(true);
        }

        /// <summary>
        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_check_all_Click(object sender, EventArgs e)
        {
            _db_info_list?.Where(x => !x.is_check)?.ToList()?.ForEach(x => x.is_check = true);
            data_update();
        }

        /// <summary>
        /// 反选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_check_un_Click(object sender, EventArgs e)
        {
            _db_info_list?.ForEach(x => x.is_check = !x.is_check);
            data_update();
        }


        /// <summary>
        /// 过滤设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_filter_Click(object sender, EventArgs e)
        {
            form_db_info_filter.ShowAndBringToFront();
        }

        /// <summary>
        /// 勾选事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgv_db_info_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgv_db_info.Columns[e.ColumnIndex].Name == "is_check")//单击复选框时
            {
                pb_finish.Focus();//切换焦点，防止数据不更新
                data_update();
            }
        }

        /// <summary>
        /// 查看最新数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_last_Click(object sender, EventArgs e)
        {
            var last_info = _db_info_list.Where(x => x.updatetime == _db_info_list.Max(x => x.updatetime)).FirstOrDefault();
            if (last_info != null)
            {
                this.dgv_db_info.FirstDisplayedScrollingRowIndex = (int)(last_info.num - 1);
            }
        }

        /// <summary>
        /// 显示所有
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_show_total_Click(object sender, EventArgs e)
        {
            show_fail = false;
            data_update();
        }

        /// <summary>
        /// 显示失败
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_show_fail_Click(object sender, EventArgs e)
        {
            show_fail = true;
            data_update();
        }

        /// <summary>
        /// 获取最新记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_last_get_Click(object sender, EventArgs e)
        {
            var res = manage.last_get();
            if (!string.IsNullOrEmpty(res))
            {
                MessageBox.Show(res);
            }
        }

        /// <summary>
        /// 修改线程数量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tb_task_num_Validated(object sender, EventArgs e)
        {
            config_set();
        }

        /// <summary>
        /// 暂停/重启
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_stop_restart_Click(object sender, EventArgs e)
        {
            if_stop = !if_stop;
            if (if_stop)
            {
                //暂停
                manage.bak_start_end(null, false);
                bt_stop_restart.Text = "继续";
            }
            else
            {
                //继续
                manage.bak_start_end(null, true);
                bt_stop_restart.Text = "暂停";
            }
        }
    }
}
